cap log close

global path = "****"

local name= "mort_nonlinear"  
local date = "052620"
global logname = "$path/`name'_`date'.log"
set matsize 11000

clear
set more off
cd "$path"
log using "$logname", replace

use "$path/Data/penalty_sample.dta", clear
rename fpl_frac magi_fpl2015
replace age_2017=0 if age_2017<0


*create control variables
gen age_45_64 = age_2017 < 65 & age_2017 >= 45

*Table: Effect of Intervention on Middle Age Mortality: Alternate Specifications

*Col 2: Reduced Form with Randomization Block Controls
reghdfe dead1718 treatment if age_45_64==1 & notall16==1, vce(cl tin) absorb(cluster1)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/mort_nonlinear_`date'.ster", replace

*col 3: Logit without Controls
estimates clear
eststo clear

logit dead1718 i.treatment if age_45_64==1 & notall16==1, cl(tin)
eststo
estadd scalar nobs e(N)
cap mat drop b
mat b = r(table)
estadd scalar pval = b[4,2]
summ `e(depvar)' if e(sample)&treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/mort_nonlinear_`date'.ster", append
margins treatment, atmeans post 
eststo
estimates save "$path/mort_nonlinear_`date'.ster", append

*col 5: cox without controls 
*set up variables for survival analysis
sort yod mod
cap drop period
egen period = group(yod mod) if dead1718 == 1
replace period = 24 if dead1718 == 0
*cox model
stset period, failure(dead1718==1)
stcox treatment if age_45_64==1 & notall16==1 & dead16 == 0, cl(tin) 
eststo
estadd scalar nobs = e(N_sub)
cap mat drop b
mat b = r(table)
estadd scalar pval = b[4,1]
estimates save "$path/mort_nonlinear_`date'.ster", append

*col 7: log rank test
cap program drop lr_treatment
program lr_treatment, eclass
	syntax varlist
	sts test treatment if age_45_64==1 & notall16==1 & dead16 == 0, logrank
	mat results_logrank = r(chi2), chi2tail(r(df), r(chi2))
	mat coln results_logrank = chi2, pval
	eret mat results_logrank = results_logrank
end
lr_treatment treatment
eststo
estimates save "$path/mort_nonlinear_`date'.ster", append

log close

